package com.shanghaionstar.navi.base;

import android.support.v7.widget.RecyclerView;

import com.shanghaionstar.navi.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerAdapter<D, V extends BaseViewHolder> extends RecyclerView.Adapter<V> {

    protected List<D> mDataSet = new ArrayList<>();

    protected OnItemClickListener<D> mItemClickListener;

    public List<D> getDataSet() {
        return mDataSet;
    }

    public void setOnItemClickListener(OnItemClickListener<D> mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public final void onBindViewHolder(V viewHolder, int position) {
        final D item = getItem(position);
        bindDataToViewHolder(viewHolder, position, item);
    }

    public void remove(int position) {
        mDataSet.remove(position);
        notifyDataSetChanged();
    }

    public void addItems(List<D> items) {
        items.removeAll(mDataSet);
        mDataSet.addAll(items);
        notifyDataSetChanged();
    }

    public void setData(List<D> data){
        mDataSet.clear();
        mDataSet.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        mDataSet.clear();
        notifyDataSetChanged();
    }

    public D getItem(int position){
        return mDataSet.get(position);
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    protected abstract void bindDataToViewHolder(V viewHolder, int position, D item);
}
